/* *****************************************************************************
 *
 *	danizone.ch
 *
 *	Developer:		mda010
 *	Creation date:	May 6, 2012 10:09:17 PM
 *	
 *	Project name:	HomegrownCuisine
 *	File name:		AdminController.java
 *
 *	Copyrights (C) 2012 @danizone.ch
 *
 *   This program is free software; you can redistribute it and/or modify
 *	it under the terms of the GNU General Public License as published by
 *	the Free Software Foundation; either version 2 of the License, or
 *	(at your option) any later version.
 *
 *	This program is distributed in the hope that it will be useful,
 *	but WITHOUT ANY WARRANTY; without even the implied warranty of
 *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 *	GNU General Public License for more details.
 *
 *	Change history:	[developer]; [date];[reason for change]
 *
 *					mda010; May 6, 2012; created
 *
 ***************************************************************************** */
package ch.zhaw.hgc.controllers;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.support.ByteArrayMultipartFileEditor;
import org.springframework.web.servlet.ModelAndView;

import ch.zhaw.hgc.entities.PlantingInstruction;
import ch.zhaw.hgc.services.IPlantingService;

/**
 * @author Remo Zollinger 
 * Description: Provides the basic functionality to add a recipe.
 */
@Controller
@RequestMapping("/admin/addPlanting")
public class AddPlantingController {
	
	private static final Logger logger = Logger.getLogger(AddPlantingController.class);
	
	@Autowired
	private IPlantingService plantingService;

	@InitBinder
	protected void initBinder(HttpServletRequest request,
			ServletRequestDataBinder binder) throws ServletException {
		// to actually be able to convert Multipart instance to a String
		// we have to register a custom editor
		binder.registerCustomEditor(byte[].class,
				new ByteArrayMultipartFileEditor());
		//binder.registerCustomEditor(String.class, new StringMultipartFileEditor());
		// now Spring knows how to handle multipart object and convert them
	}
	
	/**
	 * Get Method for planting instruction.
	 * @param plantingInstruction a planting instruction
	 * @return the Model view
	 */
	@RequestMapping(method = RequestMethod.GET)
	public ModelAndView get(PlantingInstruction plantingInstruction) {
		
		ModelAndView model = new ModelAndView("admin/addPlanting");
		model.addObject("plantingInstruction", plantingInstruction);
		model.addObject("message", "");		
		
		logger.info("get()...");

		return model;
	}

	/**
	 * Post Method adding a planting instruction.
	 * @param plantingInstruction a planting instruction
	 * @return the Model view
	 */
	@RequestMapping(method = RequestMethod.POST)
	public ModelAndView submit(@ModelAttribute("plantingInstruction") PlantingInstruction plantingInstruction) {				
				
		User user = (User)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		plantingInstruction.setAuthor(user.getUsername());
		
		plantingService.save(plantingInstruction);
		
		ModelAndView model = new ModelAndView("admin/addPlanting");				
		model.addObject("plantingInstruction", new PlantingInstruction());
		model.addObject("message", "Successfully saved");
	
		logger.info("submit()...");
		
		return model;
	}
}